package com.xinli.business.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinli.business.domain.LfPostFabulous;
import com.xinli.common.utils.DateUtils;
import com.xinli.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xinli.business.mapper.LfCommentThumbsMapper;
import com.xinli.business.domain.LfCommentThumbs;
import com.xinli.business.service.ILfCommentThumbsService;

/**
 * 评论点赞Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-29
 */
@Service
public class LfCommentThumbsServiceImpl extends ServiceImpl<LfCommentThumbsMapper,LfCommentThumbs>  implements ILfCommentThumbsService
{
    @Autowired
    private LfCommentThumbsMapper lfCommentThumbsMapper;

    /**
     * 查询评论点赞
     *
     * @param id 评论点赞主键
     * @return 评论点赞
     */
    @Override
    public LfCommentThumbs selectLfCommentThumbsById(Long id)
    {
        return lfCommentThumbsMapper.selectLfCommentThumbsById(id);
    }

    /**
     * 查询评论点赞列表
     *
     * @param lfCommentThumbs 评论点赞
     * @return 评论点赞
     */
    @Override
    public List<LfCommentThumbs> selectLfCommentThumbsList(LfCommentThumbs lfCommentThumbs)
    {
        return lfCommentThumbsMapper.selectLfCommentThumbsList(lfCommentThumbs);
    }

    /**
     * 新增评论点赞
     *
     * @param lfCommentThumbs 评论点赞
     * @return 结果
     */
    @Override
    public int insertLfCommentThumbs(LfCommentThumbs lfCommentThumbs)
    {
                lfCommentThumbs.setCreateTime(DateUtils.getNowDate());
            return lfCommentThumbsMapper.insertLfCommentThumbs(lfCommentThumbs);
    }

    /**
     * 修改评论点赞
     *
     * @param lfCommentThumbs 评论点赞
     * @return 结果
     */
    @Override
    public int updateLfCommentThumbs(LfCommentThumbs lfCommentThumbs)
    {
        return lfCommentThumbsMapper.updateLfCommentThumbs(lfCommentThumbs);
    }

    /**
     * 批量删除评论点赞
     *
     * @param ids 需要删除的评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteLfCommentThumbsByIds(Long[] ids)
    {
        return lfCommentThumbsMapper.deleteLfCommentThumbsByIds(ids);
    }

    /**
     * 删除评论点赞信息
     *
     * @param id 评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteLfCommentThumbsById(Long id)
    {
        return lfCommentThumbsMapper.deleteLfCommentThumbsById(id);
    }




    @Override
    public Boolean followPl(Long cId) {
        // 获取当前用户ID（假设从安全上下文中获取）
        String userId = SecurityUtils.getUserIdStr();
        // 检查是否已关注
        QueryWrapper<LfCommentThumbs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_id", cId)
                .eq("uid", userId);
        List<LfCommentThumbs>   existingDz = this.list(queryWrapper);

        if (CollectionUtil.isEmpty(existingDz)) {
            // 未关注，新增关注记录
            LfCommentThumbs newFollow = new LfCommentThumbs();
            newFollow.setCId(cId);
            newFollow.setUid(Long.valueOf(userId));
            newFollow.setCreateTime(DateUtils.getNowDate());
            this.insertLfCommentThumbs(newFollow);
            return  true;
        } else {
            // 已关注，取消关注（删除记录）
            this.remove(queryWrapper);
            return  false;
        }
    }



    @Override
    public Boolean isDz(Long cId) {
        // 获取当前用户ID（假设从安全上下文中获取）
        String userId = SecurityUtils.getUserIdStr();
        // 检查是否已关注
        QueryWrapper<LfCommentThumbs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_id", cId)
                .eq("uid", userId);
        long count = this.count(queryWrapper);
        if (count>0) {
            return  true;
        } else {
            return  false;
        }
    }


    @Override
    public Long dzCount(Long cId) {
        // 获取当前用户ID（假设从安全上下文中获取）
        // 检查是否已关注
        QueryWrapper<LfCommentThumbs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_id", cId);
        long count = this.count(queryWrapper);
        return count;

    }
}
